package com.example.getsystempropclient;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.getsystempropdemo.IGetSystemPropInterface;

public class MainActivity extends AppCompatActivity {

    // log tag
    private static final String TAG = "GetSystemClient";

    // TextView 控件
    private TextView mTextViewDeviceVersion  = null;
    private TextView mTextViewAndroidVersion = null;
    private TextView mTextViewSystemVersion  = null;
    private TextView mTextViewUserDefined    = null;
    private ScrollTestView mTextViewComplieTime  = null;
    private ScrollTestView mTextViewGetCurrentTime  = null;

    // EditText 控件
    private EditText mEditTextSetInfo = null;

    // Button 控件
    private Button mButtonGetInfo  = null;
    private Button mButtonSetInfo  = null;
    private Button mButtonClenInfo = null;

    // AIDL
    private IGetSystemPropInterface mBinder = null;

    // 标志位
    private boolean mBound = false;

    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            DEBUG("onServiceConnected");
            mBound = true;

            mBinder = IGetSystemPropInterface.Stub.asInterface(service);

            getInfo();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            DEBUG("onServiceDisconnected");
            mBound = false;
            mBinder = null;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DEBUG("onCreate");

        InitView();
    }

    @Override
    protected void onStop() {
        super.onStop();
        DEBUG("onStop");
        if (mBound) {
            unbindService(mServiceConnection);
        }
    }

    private void InitView() {
        DEBUG("InitView");

        setTitle(R.string.app_title);

        mTextViewDeviceVersion  = (TextView) findViewById(R.id.tv_device_version);
        mTextViewAndroidVersion = (TextView) findViewById(R.id.tv_android_version);
        mTextViewSystemVersion  = (TextView) findViewById(R.id.tv_system_version);
        mTextViewComplieTime    = (ScrollTestView) findViewById(R.id.tv_compile_time);
        mTextViewGetCurrentTime = (ScrollTestView) findViewById(R.id.tv_get_current_time);
        mTextViewUserDefined    = (TextView) findViewById(R.id.tv_user_defind);

        mEditTextSetInfo = (EditText) findViewById(R.id.et_set_info);

        mButtonGetInfo  = (Button) findViewById(R.id.btn_get_info);
        mButtonSetInfo  = (Button) findViewById(R.id.btn_set_info);
        mButtonClenInfo = (Button) findViewById(R.id.btn_clean_info);



        if (isInitView()) {
            mButtonGetInfo.setOnClickListener(new ButtonOnClickListener());
            mButtonSetInfo.setOnClickListener(new ButtonOnClickListener());
            mButtonClenInfo.setOnClickListener(new ButtonOnClickListener());
        }
    }

    // 显示信息
    private void getInfo() {
        if (mBinder != null && isInitView()) {
            try {
                mTextViewDeviceVersion.setText(mBinder.getDeviceVersion());
                mTextViewAndroidVersion.setText(mBinder.getAndroidVersion());
                mTextViewSystemVersion.setText(mBinder.getSystemVersion());
                mTextViewComplieTime.setText(mBinder.getCompileTime());
                mTextViewGetCurrentTime.setText(mBinder.getCurrentTime());
                mTextViewUserDefined.setText(mBinder.getUserDefined());
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    // 设置属性信息
    private void setInfo() {
        DEBUG("setInfo");
        if (mBound && isInitView() && mBinder != null) {
            try {
                String info = mEditTextSetInfo.getText().toString();
                DEBUG("setInfo: " + info);
                mBinder.setInfo(info);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    // 初始化信息
    private void cleanInfo() {
        DEBUG("cleanInfo");
        String info = "";
        if (isInitView()) {
            mTextViewDeviceVersion.setText(info);
            mTextViewAndroidVersion.setText(info);
            mTextViewSystemVersion.setText(info);
            mTextViewComplieTime.setText(info);
            mTextViewGetCurrentTime.setText(info);
            mTextViewUserDefined.setText(info);

            mEditTextSetInfo.setText(info);
        }
    }

    // View 控件是否已经初始化
    private boolean isInitView() {
        boolean status = false;
        if (mTextViewDeviceVersion != null && mTextViewAndroidVersion != null &&
                mTextViewSystemVersion != null && mTextViewComplieTime != null &&
                mTextViewGetCurrentTime != null && mTextViewUserDefined != null &&
                mEditTextSetInfo != null &&
                mButtonGetInfo != null && mButtonSetInfo != null && mButtonClenInfo != null) {
            status = true;
        }
        return status;
    }

    class ButtonOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_get_info:
                    DEBUG("Click GetInfo Button.");
                    if (mBound) {
                        getInfo();
                    } else {
                        Intent intent = new Intent("com.example.getsystempropdemo.GetSystemPropService");
                        intent.setPackage("com.example.getsystempropdemo");
                        bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
                    }
                    break;
                case R.id.btn_set_info:
                    DEBUG("Click SetInfo Button.");
                    setInfo();
                    break;
                case R.id.btn_clean_info:
                    DEBUG("Click CleanInfo Button.");
                    if (mBound) {
                        mBound = false;
                        unbindService(mServiceConnection);
                    }
                    cleanInfo();
                    break;
                default:
                    DEBUG("Error!!! Click Other Button.");
                    break;
            }
        }
    }

    // Log 打印
    private void DEBUG(String info) {
        Log.d(TAG, info);
    }
}
